<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/2/22
 * Time: 14:45
 */

namespace App\Http\Controllers\Setting;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\ApprovalModel;
use App\Http\Model\ApprovalProcessModel;
use App\Http\Model\AttendanceModel;
use App\Http\Model\HandymanAccountsModel;
use App\Http\Model\LivingModel;
use App\Http\Model\LoanModel;
use App\Http\Model\SupplierOrdersModel;
use App\Http\Model\WarehouseLogModel;
use App\Http\Model\WorkmanAccountsModel;
use App\Http\Model\WorkmanAssignmentModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ApprovalResultController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function groupConsumeReject(Request $request)
    {
        $rules = [
            'logId' => 'required|integer',
            'projectId' => 'required|integer',
            'month' => 'required|date_format:Y-m',
        ];
        $message = [
            'logId.required' => '获取记录参数失败',
            'logId.integer' => '记录参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'month.date_format' => '起始时间格式不正确',
        ];
        $param = $request->only('logId', 'projectId', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseLogModel = new WarehouseLogModel();
            $warehouseLogModel->update(['id' => $param['logId']], ['status' => 3]);

            $approvalModel = new ApprovalModel();
            $approvalProcessModel = new ApprovalProcessModel();
            $approval = $approvalModel->info(['projectId' => $param['projectId'], 'month' => $param['month']]);
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['approvalId' => $approval['id'], 'adminId' => $adminId]);
            $result = json_decode($info['result'], true);
            $result['groupConsume'] = 2;
            $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
        } else {
            throw new ApiException($validator->errors()->first(), 160101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function groupConsumeConsent(Request $request)
    {
        $rules = [
            'approvalId' => 'required|integer',
            'processId' => 'required|integer',
            'type' => 'required|in:7',
        ];
        $message = [
            'approvalId.required' => '获取参数失败',
            'approvalId.integer' => '参数类型不正确',
            'processId.required' => '获取参数失败',
            'processId.integer' => '参数类型不正确',
            'type.required' => '获取类型失败',
            'type.in' => '类型参数不正确',
        ];
        $param = $request->only('approvalId', 'processId', 'type');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseLogModel = new WarehouseLogModel();
            $approvalModel = new ApprovalModel();
            $approval = $approvalModel->info(['id' => $param['approvalId']]);
            $startTime = $approval['startTime'];
            $endTime = $approval['endTime'];
            $warehouseLogModel->update([
                'projectId' => $approval['projectId'],
//                'sourceGroupId'=>$param['sourceGroupId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 3,
                'type' => $param['type']
            ], ['status' => 1]);

            $approvalProcessModel = new ApprovalProcessModel();
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['id' => $param['processId']]);
            $result = json_decode($info['result'], true);
            $result['groupConsume'] = 1;

            $a = 1;
            foreach ($result as $r) {
                $a *= $r;
            }
            if ($a == 1) {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result), 'status' => 1,'adminId'=>$adminId,'updateTime'=>date('Y-m-d H:i:s')]);
            } else {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
            }
            $this->approvalConsent($param['approvalId'],$param['processId']);
        } else {
            throw new ApiException($validator->errors()->first(), 160201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function consumeReject(Request $request)
    {
        $rules = [
            'logId' => 'required|integer',
            'projectId' => 'required|integer',
            'month' => 'required|date_format:Y-m',
        ];
        $message = [
            'logId.required' => '获取记录参数失败',
            'logId.integer' => '记录参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'month.date_format' => '起始时间格式不正确',
        ];
        $param = $request->only('logId', 'projectId', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseLogModel = new WarehouseLogModel();
            $warehouseLogModel->update(['id' => $param['logId']], ['status' => 3]);

            $approvalModel = new ApprovalModel();
            $approvalProcessModel = new ApprovalProcessModel();
            $approval = $approvalModel->info(['projectId' => $param['projectId'], 'month' => $param['month']]);
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['approvalId' => $approval['id'], 'adminId' => $adminId]);
            $result = json_decode($info['result'], true);
            $result['consume'] = 2;
            $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
        } else {
            throw new ApiException($validator->errors()->first(), 160301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function consumeConsent(Request $request)
    {
        $rules = [
            'approvalId' => 'required|integer',
            'processId' => 'required|integer',
            'type' => 'required|in:4',
        ];
        $message = [
            'approvalId.required' => '获取参数失败',
            'approvalId.integer' => '参数类型不正确',
            'processId.required' => '获取参数失败',
            'processId.integer' => '参数类型不正确',
            'type.required' => '获取类型失败',
            'type.in' => '类型参数不正确',
        ];
        $param = $request->only('approvalId', 'processId', 'type');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseLogModel = new WarehouseLogModel();
            $approvalModel = new ApprovalModel();
            $approval = $approvalModel->info(['id' => $param['approvalId']]);
            $startTime = $approval['startTime'];
            $endTime = $approval['endTime'];
            $warehouseLogModel->update([
                'projectId' => $approval['projectId'],
//                'sourceGroupId'=>$param['sourceGroupId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 2,
                'type' => $param['type']
            ], ['status' => 1]);

            $approvalProcessModel = new ApprovalProcessModel();
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['id' => $param['processId']]);
            $result = json_decode($info['result'], true);
            $result['consume'] = 1;

            $a = 1;
            foreach ($result as $r) {
                $a *= $r;
            }
            if ($a == 1) {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result), 'status' => 1,'adminId'=>$adminId,'updateTime'=>date('Y-m-d H:i:s')]);
            } else {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
            }
            $this->approvalConsent($param['approvalId'],$param['processId']);
        } else {
            throw new ApiException($validator->errors()->first(), 160401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function supplierReject(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'projectId' => 'required|integer',
            'month' => 'required|date_format:Y-m',
        ];
        $message = [
            'id.required' => '获取参数失败',
            'id.integer' => '参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'month.date_format' => '起始时间格式不正确',
        ];
        $param = $request->only('id', 'projectId', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $supplierOrdersModel = new SupplierOrdersModel();
            $supplierOrdersModel->update(['id' => $param['id']], ['status' => 3]);

            $approvalModel = new ApprovalModel();
            $approvalProcessModel = new ApprovalProcessModel();
            $approval = $approvalModel->info(['projectId' => $param['projectId'], 'month' => $param['month']]);
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['approvalId' => $approval['id'], 'adminId' => $adminId]);
            $result = json_decode($info['result'], true);
            $result['supplier'] = 2;
            $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
        } else {
            throw new ApiException($validator->errors()->first(), 160501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function supplierConsent(Request $request)
    {
        $rules = [
            'approvalId' => 'required|integer',
            'processId' => 'required|integer',
        ];
        $message = [
            'approvalId.required' => '获取参数失败',
            'approvalId.integer' => '参数类型不正确',
            'processId.required' => '获取参数失败',
            'processId.integer' => '参数类型不正确',
        ];
        $param = $request->only('approvalId', 'processId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $supplierOrdersModel = new SupplierOrdersModel();
            $approvalModel = new ApprovalModel();
            $approval = $approvalModel->info(['id' => $param['approvalId']]);
            $startTime = $approval['startTime'];
            $endTime = $approval['endTime'];
            $supplierOrdersModel->update([
                'projectId' => $approval['projectId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 2,
            ], ['status' => 1]);

            $approvalProcessModel = new ApprovalProcessModel();
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['id' => $param['processId']]);
            $result = json_decode($info['result'], true);
            $result['supplier'] = 1;

            $a = 1;
            foreach ($result as $r) {
                $a *= $r;
            }
            if ($a == 1) {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result), 'status' => 1,'adminId'=>$adminId,'updateTime'=>date('Y-m-d H:i:s')]);
            } else {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
            }
            $this->approvalConsent($param['approvalId'],$param['processId']);
        } else {
            throw new ApiException($validator->errors()->first(), 160601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function livingReject(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'projectId' => 'required|integer',
            'month' => 'required|date_format:Y-m',
        ];
        $message = [
            'id.required' => '获取参数失败',
            'id.integer' => '参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'month.date_format' => '起始时间格式不正确',
        ];
        $param = $request->only('id', 'projectId', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $livingModel = new LivingModel();
            $livingModel->update(['id' => $param['id']], ['status' => 3]);

            $approvalModel = new ApprovalModel();
            $approvalProcessModel = new ApprovalProcessModel();
            $approval = $approvalModel->info(['projectId' => $param['projectId'], 'month' => $param['month']]);
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['approvalId' => $approval['id'], 'adminId' => $adminId]);
            $result = json_decode($info['result'], true);
            $result['living'] = 2;
            $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
        } else {
            throw new ApiException($validator->errors()->first(), 160501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function livingConsent(Request $request)
    {
        $rules = [
            'approvalId' => 'required|integer',
            'processId' => 'required|integer',
        ];
        $message = [
            'approvalId.required' => '获取参数失败',
            'approvalId.integer' => '参数类型不正确',
            'processId.required' => '获取参数失败',
            'processId.integer' => '参数类型不正确',
        ];
        $param = $request->only('approvalId', 'processId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $livingModel = new LivingModel();
            $approvalModel = new ApprovalModel();
            $approval = $approvalModel->info(['id' => $param['approvalId']]);
            $startTime = $approval['startTime'];
            $endTime = $approval['endTime'];
            $livingModel->update([
                'projectId' => $approval['projectId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 2,
            ], ['status' => 1]);

            $approvalProcessModel = new ApprovalProcessModel();
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['id' => $param['processId']]);
            $result = json_decode($info['result'], true);
            $result['living'] = 1;

            $a = 1;
            foreach ($result as $r) {
                $a *= $r;
            }
            if ($a == 1) {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result), 'status' => 1,'adminId'=>$adminId,'updateTime'=>date('Y-m-d H:i:s')]);
            } else {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
            }
            $this->approvalConsent($param['approvalId'],$param['processId']);
        } else {
            throw new ApiException($validator->errors()->first(), 160601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function loanReject(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'projectId' => 'required|integer',
            'month' => 'required|date_format:Y-m',
        ];
        $message = [
            'id.required' => '获取参数失败',
            'id.integer' => '参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'month.date_format' => '起始时间格式不正确',
        ];
        $param = $request->only('id', 'projectId', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanModel = new LoanModel();
            $loanModel->update(['id' => $param['id']], ['status' => 3]);

            $approvalModel = new ApprovalModel();
            $approvalProcessModel = new ApprovalProcessModel();
            $approval = $approvalModel->info(['projectId' => $param['projectId'], 'month' => $param['month']]);
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['approvalId' => $approval['id'], 'adminId' => $adminId]);
            $result = json_decode($info['result'], true);
            $result['loan'] = 2;
            $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
        } else {
            throw new ApiException($validator->errors()->first(), 160501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function loanConsent(Request $request)
    {
        $rules = [
            'approvalId' => 'required|integer',
            'processId' => 'required|integer',
        ];
        $message = [
            'approvalId.required' => '获取参数失败',
            'approvalId.integer' => '参数类型不正确',
            'processId.required' => '获取参数失败',
            'processId.integer' => '参数类型不正确',
        ];
        $param = $request->only('approvalId', 'processId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanModel = new LoanModel();
            $approvalModel = new ApprovalModel();
            $approval = $approvalModel->info(['id' => $param['approvalId']]);
            $startTime = $approval['startTime'];
            $endTime = $approval['endTime'];
            $loanModel->update([
                'projectId' => $approval['projectId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 2,
            ], ['status' => 1]);

            $approvalProcessModel = new ApprovalProcessModel();
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['id' => $param['processId']]);
            $result = json_decode($info['result'], true);
            $result['loan'] = 1;

            $a = 1;
            foreach ($result as $r) {
                $a *= $r;
            }
            if ($a == 1) {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result), 'status' => 1,'adminId'=>$adminId,'updateTime'=>date('Y-m-d H:i:s')]);
            } else {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
            }

            $this->approvalConsent($param['approvalId'],$param['processId']);
        } else {
            throw new ApiException($validator->errors()->first(), 160601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function attendanceReject(Request $request)
    {
        $rules = [
            'employeeId' => 'required|integer',
            'projectId' => 'required|integer',
            'month' => 'required|date_format:Y-m',
        ];
        $message = [
            'employeeId.required' => '获取项目参数失败',
            'employeeId.integer' => '项目参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'month.date_format' => '起始时间格式不正确',
        ];
        $param = $request->only('employeeId', 'projectId', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $attendanceModel = new AttendanceModel();
            $startTime = date($param['month'] . '-01 00:00:00');
            $endTime = date($param['month'] . '-t 23:59:59');
            $attendanceModel->update(['employeeId' => $param['employeeId'], 'startTime' => $startTime, 'endTime' => $endTime], ['status' => 3]);

            $approvalModel = new ApprovalModel();
            $approvalProcessModel = new ApprovalProcessModel();
            $approval = $approvalModel->info(['projectId' => $param['projectId'], 'month' => $param['month']]);
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['approvalId' => $approval['id'], 'adminId' => $adminId]);
            $result = json_decode($info['result'], true);
            $result['attendance'] = 4;
            $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
        } else {
            throw new ApiException($validator->errors()->first(), 160701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function attendanceConsent(Request $request)
    {
        $rules = [
            'approvalId' => 'required|integer',
            'processId' => 'required|integer',
        ];
        $message = [
            'approvalId.required' => '获取参数失败',
            'approvalId.integer' => '参数类型不正确',
            'processId.required' => '获取参数失败',
            'processId.integer' => '参数类型不正确',
        ];
        $param = $request->only('approvalId', 'processId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $attendanceModel = new AttendanceModel();
            $approvalModel = new ApprovalModel();
            $approval = $approvalModel->info(['id' => $param['approvalId']]);
            $startTime = $approval['startTime'];
            $endTime = $approval['endTime'];
            $attendanceModel->update([
                'projectId' => $approval['projectId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 1,
            ], ['status' => 2]);

            $approvalProcessModel = new ApprovalProcessModel();
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['id' => $param['processId']]);
            $result = json_decode($info['result'], true);
            $result['attendance'] = 1;

            $a = 1;
            foreach ($result as $r) {
                $a *= $r;
            }
            if ($a == 1) {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result), 'status' => 1,'adminId'=>$adminId,'updateTime'=>date('Y-m-d H:i:s')]);
            } else {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
            }

            $this->approvalConsent($param['approvalId'],$param['processId']);
        } else {
            throw new ApiException($validator->errors()->first(), 160801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function handymanAccountsReject(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'projectId' => 'required|integer',
            'month' => 'required|date_format:Y-m',
        ];
        $message = [
            'id.required' => '获取参数失败',
            'id.integer' => '参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'month.date_format' => '起始时间格式不正确',
        ];
        $param = $request->only('id', 'projectId', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $handymanAccountsModel = new HandymanAccountsModel();
            $handymanAccountsModel->update(['id' => $param['id']], ['status' => 3]);

            $approvalModel = new ApprovalModel();
            $approvalProcessModel = new ApprovalProcessModel();
            $approval = $approvalModel->info(['projectId' => $param['projectId'], 'month' => $param['month']]);
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['approvalId' => $approval['id'], 'adminId' => $adminId]);
            $result = json_decode($info['result'], true);
            $result['handymanAccounts'] = 2;
            $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
        } else {
            throw new ApiException($validator->errors()->first(), 160501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function handymanAccountsConsent(Request $request)
    {
        $rules = [
            'approvalId' => 'required|integer',
            'processId' => 'required|integer',
        ];
        $message = [
            'approvalId.required' => '获取参数失败',
            'approvalId.integer' => '参数类型不正确',
            'processId.required' => '获取参数失败',
            'processId.integer' => '参数类型不正确',
        ];
        $param = $request->only('approvalId', 'processId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $handymanAccountsModel = new HandymanAccountsModel();
            $approvalModel = new ApprovalModel();
            $approval = $approvalModel->info(['id' => $param['approvalId']]);
            $startTime = $approval['startTime'];
            $endTime = $approval['endTime'];
            $handymanAccountsModel->update([
                'projectId' => $approval['projectId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 2,
            ], ['status' => 1]);

            $approvalProcessModel = new ApprovalProcessModel();
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['id' => $param['processId']]);
            $result = json_decode($info['result'], true);
            $result['handymanAccounts'] = 1;

            $a = 1;
            foreach ($result as $r) {
                $a *= $r;
            }
            if ($a == 1) {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result), 'status' => 1,'adminId'=>$adminId,'updateTime'=>date('Y-m-d H:i:s')]);
            } else {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
            }

            $this->approvalConsent($param['approvalId'],$param['processId']);
        } else {
            throw new ApiException($validator->errors()->first(), 160601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function workmanAssignmentReject(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'projectId' => 'required|integer',
            'month' => 'required|date_format:Y-m',
        ];
        $message = [
            'id.required' => '获取参数失败',
            'id.integer' => '参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'month.date_format' => '起始时间格式不正确',
        ];
        $param = $request->only('id', 'projectId', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $workmanAssignmentModel->updateStatus(['id' => $param['id']], ['status' => 3]);

            $approvalModel = new ApprovalModel();
            $approvalProcessModel = new ApprovalProcessModel();
            $approval = $approvalModel->info(['projectId' => $param['projectId'], 'month' => $param['month']]);
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['approvalId' => $approval['id'], 'adminId' => $adminId]);
            $result = json_decode($info['result'], true);
            $result['workmanAssignment'] = 2;
            $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
        } else {
            throw new ApiException($validator->errors()->first(), 160501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function workmanAccountReject(Request $request)
    {
        $rules = [
            'employeeId' => 'required|integer',
            'projectId' => 'required|integer',
            'month' => 'required|date_format:Y-m',
        ];
        $message = [
            'employeeId.required' => '获取工人参数失败',
            'employeeId.integer' => '工人参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'month.date_format' => '起始时间格式不正确',
        ];
        $param = $request->only('employeeId', 'projectId', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAccountsModel = new WorkmanAccountsModel();
            $startTime = date($param['month'] . '-01 00:00:00');
            $endTime = date($param['month'] . '-t 23:59:59');
            $condition = [
                'projectId' => $param['projectId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 2,
                'employeeId' => $param['employeeId']
            ];
            $workmanAccountsModel->updateStatus($condition, ['status' => 2]);

            $approvalModel = new ApprovalModel();
            $approvalProcessModel = new ApprovalProcessModel();
            $approval = $approvalModel->info(['projectId' => $param['projectId'], 'month' => $param['month']]);
            $adminId = $request->session()->get('adminId');
            $info = $approvalProcessModel->info(['approvalId' => $approval['id'], 'adminId' => $adminId]);
            $result = json_decode($info['result'], true);
            $result['workmanAssignment'] = 2;
            $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
        } else {
            throw new ApiException($validator->errors()->first(), 160501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function workmanAssignmentConsent(Request $request)
    {
        $rules = [
            'approvalId' => 'required|integer',
            'processId' => 'required|integer',
        ];
        $message = [
            'approvalId.required' => '获取参数失败',
            'approvalId.integer' => '参数类型不正确',
            'processId.required' => '获取参数失败',
            'processId.integer' => '参数类型不正确',
        ];
        $param = $request->only('approvalId', 'processId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $approvalModel = new ApprovalModel();
            $approval = $approvalModel->info(['id' => $param['approvalId']]);
            $startTime = $approval['startTime'];
            $endTime = $approval['endTime'];
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $workmanAccountsModel = new WorkmanAccountsModel();

            $workmanAssignmentModel->updateStatus([
                'projectId' => $approval['projectId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 2,
            ], ['status' => 1]);
            $workmanAccountsModel->updateStatus([
                'projectId' => $approval['projectId'],
                'startTime' => $startTime,
                'endTime' => $endTime,
                'status' => 2,
            ], ['status' => 1]);

            $approvalProcessModel = new ApprovalProcessModel();
            $adminId = $request->session()->get('adminId');

            $info = $approvalProcessModel->info(['id' => $param['processId']]);
            $result = json_decode($info['result'], true);
            $result['workmanAssignment'] = 1;
            $result['workmanAccounts'] = 1;

            $a = 1;
            foreach ($result as $r) {
                $a *= $r;
            }
            if ($a == 1) {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result), 'status' => 1,'adminId'=>$adminId,'updateTime'=>date('Y-m-d H:i:s')]);
            } else {
                $approvalProcessModel->update(['id' => $info['id']], ['result' => json_encode($result)]);
            }

            $this->approvalConsent($param['approvalId'],$param['processId']);
        } else {
            throw new ApiException($validator->errors()->first(), 160601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 变更审批事项的状态
     * @param $approvalId
     * @param $processId
     * @author wanghongrui 20211019
     */
    public function approvalConsent($approvalId,$processId)
    {
        $approvalModel = new ApprovalModel();
        $approval = $approvalModel->info(['id' => $approvalId]);

        $approvalProcessModel = new ApprovalProcessModel();
        $approvalProcesses = $approvalProcessModel->lists(['approvalId' => $approvalId]);
        $a = 1;
        // 遍历所有节点，如果所有节点都为1，则审批事项变为通过
        foreach ($approvalProcesses as $process){
            $a *= $process->status;
        }

        if ($a == 1){
            $approvalModel->update(['id' => $approvalId], ['status' => 3]);
        }else{
            $approvalModel->update(['id' => $approvalId], ['status' => 2]);
        }

    }
}